#include "comsettingwidget.h"
#include "ui_comsettingwidget.h"
#include <QDebug>
#include "common/common.h"
#include <QTextCodec>

comsettingwidget::comsettingwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::comsettingwidget)
{
    ui->setupUi(this);
    mTc = TcpClient::getInstanse();

    init();
}

comsettingwidget::~comsettingwidget()
{
    delete ui;
}

void comsettingwidget::init()
{
    mSp = new QSerialPort();
    foreach (const QSerialPortInfo& sp, QSerialPortInfo::availablePorts()) {
        ui->cbx_sp->addItem(sp.portName());
    }
}

void comsettingwidget::on_pushButton_clicked()
{
    if(mSp->isOpen())
    {
        mSp->clear();
        mSp->close();
        ui->pushButton->setText("打开");
        return;
    }
    ui->pushButton->setText("关闭");
    mSp->setPortName(ui->cbx_sp->currentText());
    if(!mSp->open(QIODevice::ReadWrite))
    {
        qDebug()<<"打开失败!";
        return;
    }
    qDebug()<<"串口打开成功!";
    switch (ui->cbx_braud->currentIndex()) {
    case 0:
        mSp->setBaudRate(QSerialPort::Baud1200);
        break;
    case 1:
        mSp->setBaudRate(QSerialPort::Baud9600);
        break;
    case 2:
        mSp->setBaudRate(QSerialPort::Baud19200);
        break;
    case 3:
        mSp->setBaudRate(QSerialPort::Baud115200);
        break;
    default:
        break;
    }

    switch (ui->cbx_databit->currentIndex()) {
    case 0:
        mSp->setDataBits(QSerialPort::Data8);
        break;
    case 1:
        mSp->setDataBits(QSerialPort::Data8);
        break;
    default:
        break;
    }
    mSp->setParity(QSerialPort::NoParity);
    mSp->setFlowControl(QSerialPort::NoFlowControl);
    switch (ui->cbx_stopbit->currentIndex()) {
    case 0:
        mSp->setStopBits(QSerialPort::OneStop);
        break;
    case 1:
        mSp->setStopBits(QSerialPort::TwoStop);
        break;
    default:
        break;
    }
    connect(mSp,SIGNAL(readyRead()),this,SLOT(readData()));
}

void comsettingwidget::readData()
{
    QByteArray arr = mSp->readAll();
    QTextCodec * utf8 =QTextCodec::codecForName("UTF-8");
    QTextCodec * gbk =QTextCodec::codecForName("GBK");
    QString str /*= QString(arr)*/;
    str = gbk->toUnicode(arr);qDebug()<<str;
    str = utf8->fromUnicode(str.toUtf8());
    ui->plainTextEdit->insertPlainText(str);
}

void comsettingwidget::on_pushButton_2_clicked()
{
#if QT_VERSION < 0X050000
    platform_sys_info_c::set_sys_ip_addr(ui->le_ip->text(),ui->le_mask->text(),ui->le_gateway->text());
#endif
}

//void comsettingwidget::onAccepted()
//{
    
//    QString ip = mSettingDlg->getIp();
//    int port = mSettingDlg->getPort();
//    if(mTc->getState()==false)
//    {
//        mTc->connectToHost(ip,port);
//    }
//    else {qDebug()<<"true";
//        mTc->disConnectFromHost();
//    }

//}

void comsettingwidget::on_btn_setserver_clicked()
{
    mSettingDlg = new ServerSetting(this);
    //connect(mSettingDlg,SIGNAL(accepted()),this,SLOT(onAccepted()));
    mSettingDlg->exec();
}

void comsettingwidget::on_btn_back_clicked()
{
    this->deleteLater();
}
